package com.cl.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

import com.cl.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.cl.annotation.IgnoreAuth;
import com.cl.annotation.SysLog;

import com.cl.entity.ExamrecordEntity;
import com.cl.entity.view.ExamrecordView;

import com.cl.service.ExamrecordService;
import com.cl.service.TokenService;
import com.cl.utils.PageUtils;
import com.cl.utils.R;
import com.cl.utils.MPUtil;
import com.cl.utils.MapUtils;
import com.cl.utils.CommonUtil;

/**
 * 考试记录
 * 后端接口
 *
 * @author
 * @email
 * @date 2025-02-18 12:38:56
 */
@RestController
@RequestMapping("/examrecord")
public class ExamrecordController {
    @Autowired
    private ExamrecordService examrecordService;


    /**
     * 考试记录接口
     */
    @RequestMapping("/groupby")
    public R page2(@RequestParam Map<String, Object> params, ExamrecordEntity examrecord, HttpServletRequest request) {
        if (request.getSession().getAttribute("role").toString().equals("学生")) {
            examrecord.setUserid((Long) request.getSession().getAttribute("userId"));
        }
        EntityWrapper<ExamrecordEntity> ew = new EntityWrapper<ExamrecordEntity>();
        PageUtils page = examrecordService.queryPageGroupBy(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, examrecord), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 选项统计接口
     */
    @RequestMapping("/options/num")
    public R optionsNum(@RequestParam Map<String, Object> params, ExamrecordEntity examrecord, HttpServletRequest request) {
        EntityWrapper<ExamrecordEntity> ew = new EntityWrapper<ExamrecordEntity>();
        PageUtils page = examrecordService.queryPageOptionsNum(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, examrecord), params), params));
        return R.ok().put("data", page);
    }


    /**
     * 后台列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, ExamrecordEntity examrecord,
                  HttpServletRequest request) {
        if (request.getSession().getAttribute("role").toString().equals("学生")) {
            examrecord.setUserid((Long) request.getSession().getAttribute("userId"));
        }
        EntityWrapper<ExamrecordEntity> ew = new EntityWrapper<ExamrecordEntity>();


        PageUtils page = examrecordService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, examrecord), params), params));
        return R.ok().put("data", page);
    }


    /**
     * 前端列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, ExamrecordEntity examrecord,
                  HttpServletRequest request) {
        if (!request.getSession().getAttribute("role").toString().equals("管理员")) {
            examrecord.setUserid((Long) request.getSession().getAttribute("userId"));
        }
        EntityWrapper<ExamrecordEntity> ew = new EntityWrapper<ExamrecordEntity>();

        PageUtils page = examrecordService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, examrecord), params), params));
        return R.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/lists")
    public R list(ExamrecordEntity examrecord) {
        EntityWrapper<ExamrecordEntity> ew = new EntityWrapper<ExamrecordEntity>();
        ew.allEq(MPUtil.allEQMapPre(examrecord, "examrecord"));
        return R.ok().put("data", examrecordService.selectListView(ew));
    }

    /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(ExamrecordEntity examrecord) {
        EntityWrapper<ExamrecordEntity> ew = new EntityWrapper<ExamrecordEntity>();
        ew.allEq(MPUtil.allEQMapPre(examrecord, "examrecord"));
        ExamrecordView examrecordView = examrecordService.selectView(ew);
        return R.ok("查询考试记录成功").put("data", examrecordView);
    }

    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        ExamrecordEntity examrecord = examrecordService.selectById(id);
        examrecord = examrecordService.selectView(new EntityWrapper<ExamrecordEntity>().eq("id", id));
        return R.ok().put("data", examrecord);
    }

    /**
     * 前端详情
     */
    @IgnoreAuth
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id) {
        ExamrecordEntity examrecord = examrecordService.selectById(id);
        examrecord = examrecordService.selectView(new EntityWrapper<ExamrecordEntity>().eq("id", id));
        return R.ok().put("data", examrecord);
    }


    /**
     * 后端保存
     */
    @RequestMapping("/save")
    @SysLog("新增考试记录")
    public R save(@RequestBody ExamrecordEntity examrecord, HttpServletRequest request) {
        //ValidatorUtils.validateEntity(examrecord);
        examrecord.setUserid((Long) request.getSession().getAttribute("userId"));
        examrecordService.insert(examrecord);
        return R.ok();
    }

    /**
     * 前端保存
     */
    @SysLog("新增考试记录")
    @RequestMapping("/add")
    public R add(@RequestBody ExamrecordEntity examrecord, HttpServletRequest request) {
        //ValidatorUtils.validateEntity(examrecord);
        examrecord.setUserid((Long) request.getSession().getAttribute("userId"));
        examrecordService.insert(examrecord);
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    @SysLog("修改考试记录")
    public R update(@RequestBody ExamrecordEntity examrecord, HttpServletRequest request) {
        //ValidatorUtils.validateEntity(examrecord);
        examrecordService.updateById(examrecord);//全部更新
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    @SysLog("删除考试记录")
    public R delete(@RequestBody Long[] ids) {
        examrecordService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
     * 当重新考试时，删除考生的某个试卷的所有考试记录
     */
    @RequestMapping("/deleteRecords")
    public R deleteRecords(@RequestParam Long userid, @RequestParam Long paperid) {
        examrecordService.delete(new EntityWrapper<ExamrecordEntity>().eq("paperid", paperid).eq("userid", userid));
        return R.ok();
    }
    //修改得分
    @RequestMapping("/updateScore")
    public R updateScore(@RequestBody Map<String, Object> params) {
        String userid = params.get("userid").toString();
        String paperid = params.get("paperid").toString();
        String questionid = params.get("questionid").toString();
        Long newScore = Long.parseLong(params.get("newScore").toString());
        R r = examrecordService.updateScoreByExam(userid,paperid,questionid,newScore);
        return R.ok();
    }

}
